using Microsoft.Win32;
using System.Diagnostics;
using System.IO;

namespace Glpt.Office
{
    /// <summary>
    /// Office组件类别
    /// </summary>
    public enum OfficeComponent
    {
        /// <summary>
        /// Word
        /// </summary>
        Word,
        /// <summary>
        /// Excel
        /// </summary>
        Excel,
        /// <summary>
        /// PowerPoint
        /// </summary>
        PowerPoint,
        /// <summary>
        /// Outlook
        /// </summary>
        Outlook
    }

    /// <summary>
    /// Office版本
    /// </summary>
    public class OfficeVersion
    {
        private const string RegKey = @"Software\Microsoft\Windows\CurrentVersion\App Paths";

        /// <summary>
        /// 获取Office组件路径
        /// </summary>
        /// <param name="_component">Office组件类别</param>
        /// <returns>Office组件路径</returns>
        public static string GetComponentPath(OfficeComponent _component)
        {
            string toReturn = string.Empty;
            string _key = string.Empty;

            switch (_component)
            {
                case OfficeComponent.Word:
                    _key = "winword.exe";
                    break;
                case OfficeComponent.Excel:
                    _key = "excel.exe";
                    break;
                case OfficeComponent.PowerPoint:
                    _key = "powerpnt.exe";
                    break;
                case OfficeComponent.Outlook:
                    _key = "outlook.exe";
                    break;
            }

            //looks inside CURRENT_USER:
            RegistryKey _mainKey = Registry.CurrentUser;
            try
            {
                _mainKey = _mainKey.OpenSubKey(RegKey + "\\" + _key, false);
                if (_mainKey != null)
                {
                    toReturn = _mainKey.GetValue(string.Empty).ToString();
                }
            }
            catch
            { }

            //if not found, looks inside LOCAL_MACHINE:
            _mainKey = Registry.LocalMachine;
            if (string.IsNullOrEmpty(toReturn))
            {
                try
                {
                    _mainKey = _mainKey.OpenSubKey(RegKey + "\\" + _key, false);
                    if (_mainKey != null)
                    {
                        toReturn = _mainKey.GetValue(string.Empty).ToString();
                    }
                }
                catch
                {
                }
            }

            //closing the handle:
            if (_mainKey != null)
                _mainKey.Close();

            return toReturn;
        }

        /// <summary>
        /// 获取版本号的主要版本号部分
        /// </summary>
        public static int GetMajorVersion(string _path)
        {
            int toReturn = 0;
            if (File.Exists(_path))
            {
                try
                {
                    FileVersionInfo _fileVersion = FileVersionInfo.GetVersionInfo(_path);
                    toReturn = _fileVersion.FileMajorPart;
                }
                catch
                { }
            }

            return toReturn;
        }
    }
}
